package com.lanou.sfu.songforu.activity;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.tools.MyConfig;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners;
import com.umeng.socialize.exception.SocializeException;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;

import java.util.Map;
import java.util.Set;

/**
 * Created by dllo on 15/10/9.
 */
public class EnterActivity extends Activity implements View.OnClickListener {
    UMSocialService mController = UMServiceFactory.getUMSocialService("com.umeng.login");
    private LinearLayout weixin, sina, qq;
    private LinearLayout back_btn;
    private LinearLayout register_btn;
    private Button enter_btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_layout_enter);

        weixin = (LinearLayout) findViewById(R.id.weixin);
        sina = (LinearLayout) findViewById(R.id.sina);
        qq = (LinearLayout) findViewById(R.id.qq);

        enter_btn = (Button) findViewById(R.id.enter_btn);
//        register_btn = (LinearLayout) findViewById(R.id.register_btn);


        weixin.setOnClickListener(this);
        sina.setOnClickListener(this);
        qq.setOnClickListener(this);

        enter_btn.setOnClickListener(this);
//        register_btn.setOnClickListener(this);

        back_btn = (LinearLayout) findViewById(R.id.back_btn);
        back_btn.setOnClickListener(this);

        //设置新浪SSO handler
        mController.getConfig().setSsoHandler(new SinaSsoHandler());


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.qq:
                UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(EnterActivity.this, "100424468",
                        "c7394704798a158208a74ab60104f0ba");
                qqSsoHandler.addToSocialSDK();
                mController.doOauthVerify(this, SHARE_MEDIA.QQ, new SocializeListeners.UMAuthListener() {
                    @Override
                    public void onStart(SHARE_MEDIA platform) {
                        Toast.makeText(EnterActivity.this, "授权开始", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(SocializeException e, SHARE_MEDIA platform) {
                        Toast.makeText(EnterActivity.this, "授权错误", Toast.LENGTH_SHORT).show();
                    }


                    @Override
                    public void onComplete(Bundle value, SHARE_MEDIA platform) {
                        Toast.makeText(EnterActivity.this, "授权完成", Toast.LENGTH_SHORT).show();
                        //获取相关授权信息

                        mController.getPlatformInfo(EnterActivity.this, SHARE_MEDIA.QQ, new SocializeListeners.UMDataListener() {
                            @Override
                            public void onStart() {
                                Toast.makeText(EnterActivity.this, "获取平台数据开始...", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onComplete(int status, Map<String, Object> info) {
                                if (status == 200 && info != null) {
                                    StringBuilder sb = new StringBuilder();
                                    Set<String> keys = info.keySet();
                                    for (String key : keys) {
                                        sb.append(key + "=" + info.get(key).toString() + "\r\n");
                                    }
                                    Log.d("TestData", sb.toString());
                                    String icon = info.get("profile_image_url").toString();
                                    String name = info.get("screen_name").toString();
                                    MyConfig.putSharePre(EnterActivity.this, "img", "profile_image_url", icon);
                                    MyConfig.putSharePre(EnterActivity.this, "name", "screen_name", name);
                                    String user_name = MyConfig.getSharePreStr(EnterActivity.this, "name", "screen_name");
                                    String user_img = MyConfig.getSharePreStr(EnterActivity.this, "img", "profile_image_url");
                                    if (user_name != null && !user_name.equals("0") && user_img != null && !user_img.equals("0")) {
                                        new MyThread().start();
                                    }

                                } else {
                                    Log.d("TestData", "发生错误：" + status);
                                }
                            }
                        });
                    }

                    @Override
                    public void onCancel(SHARE_MEDIA platform) {
                        Toast.makeText(EnterActivity.this, "授权取消", Toast.LENGTH_SHORT).show();
                    }
                });

                break;
            case R.id.sina:
                mController.doOauthVerify(EnterActivity.this, SHARE_MEDIA.SINA, new SocializeListeners.UMAuthListener() {
                    @Override
                    public void onError(SocializeException e, SHARE_MEDIA platform) {
                    }

                    @Override
                    public void onComplete(Bundle value, SHARE_MEDIA platform) {
                        if (value != null && !TextUtils.isEmpty(value.getString("uid"))) {
                            Toast.makeText(EnterActivity.this, "授权成功.", Toast.LENGTH_SHORT).show();

                            mController.getPlatformInfo(EnterActivity.this, SHARE_MEDIA.SINA, new SocializeListeners.UMDataListener() {
                                @Override
                                public void onStart() {
                                    Toast.makeText(EnterActivity.this, "获取平台数据开始...", Toast.LENGTH_SHORT).show();
                                }

                                @Override
                                public void onComplete(int status, Map<String, Object> info) {
                                    if (status == 200 && info != null) {
                                        StringBuilder sb = new StringBuilder();
                                        Set<String> keys = info.keySet();
                                        for (String key : keys) {
                                            sb.append(key + "=" + info.get(key).toString() + "\r\n");
                                        }
                                        Log.d("TestData", sb.toString());
                                        String icon = info.get("profile_image_url").toString();
                                        String name = info.get("screen_name").toString();
                                        MyConfig.putSharePre(EnterActivity.this, "img", "profile_image_url", icon);
                                        MyConfig.putSharePre(EnterActivity.this, "name", "screen_name", name);
                                        String user_name = MyConfig.getSharePreStr(EnterActivity.this, "name", "screen_name");
                                        String user_img = MyConfig.getSharePreStr(EnterActivity.this, "img", "profile_image_url");
                                        if (user_name != null && !user_name.equals("0") && user_img != null && !user_img.equals("0")) {
                                            new MyThread().start();
                                        }
                                    } else {
                                        Log.d("TestData", "发生错误：" + status);
                                    }
                                }
                            });


                        } else {
                            Toast.makeText(EnterActivity.this, "授权失败",
                                    Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onCancel(SHARE_MEDIA platform) {
                    }

                    @Override
                    public void onStart(SHARE_MEDIA platform) {
                    }
                });
                break;

            case R.id.weixin:
                mController.doOauthVerify(this, SHARE_MEDIA.WEIXIN, new SocializeListeners.UMAuthListener() {
                    @Override
                    public void onStart(SHARE_MEDIA platform) {
                        Toast.makeText(EnterActivity.this, "授权开始", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(SocializeException e, SHARE_MEDIA platform) {
                        Toast.makeText(EnterActivity.this, "授权错误", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete(Bundle value, SHARE_MEDIA platform) {
                        Toast.makeText(EnterActivity.this, "授权完成", Toast.LENGTH_SHORT).show();
                        //获取相关授权信息
                        mController.getPlatformInfo(EnterActivity.this, SHARE_MEDIA.WEIXIN, new SocializeListeners.UMDataListener() {
                            @Override
                            public void onStart() {
                                Toast.makeText(EnterActivity.this, "获取平台数据开始...", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onComplete(int status, Map<String, Object> info) {
                                if (status == 200 && info != null) {
                                    StringBuilder sb = new StringBuilder();
                                    Set<String> keys = info.keySet();
                                    for (String key : keys) {
                                        sb.append(key + "=" + info.get(key).toString() + "\r\n");
                                    }
                                    Log.d("TestData", sb.toString());
                                } else {
                                    Log.d("TestData", "发生错误：" + status);
                                }
                            }
                        });
                    }

                    @Override
                    public void onCancel(SHARE_MEDIA platform) {
                        Toast.makeText(EnterActivity.this, "授权取消", Toast.LENGTH_SHORT).show();
                    }
                });
                break;

            case R.id.enter_btn:
                Toast.makeText(this, "请正确输入账号密码", Toast.LENGTH_SHORT).show();
                break;
            case R.id.back_btn:
                finish();
                break;
//            case R.id.register_btn:
//                String url = "http://reg.vip.163.com/register.m";
//                Intent intent = new Intent(EnterActivity.this, WebViewActivity.class);
//                intent.putExtra("url", url);
//                startActivity(intent);
//                break;

        }
    }

    public class MyThread extends Thread {
        @Override
        public void run() {
            super.run();
            try {
                sleep(2000);
                finish();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


}
